package net.lovesrc.allin.utils;

import com.google.common.base.Splitter;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalTime;

public abstract class TimeUtils {

    public static boolean isNowMatched(String timesConfig) {
        if (StringUtils.isBlank(timesConfig)) {
            return false;
        }
        Iterable<String> times = Splitter.on(",")
                .omitEmptyStrings()
                .trimResults()
                .split(timesConfig);
        LocalTime now = LocalTime.now();
        for (String timeConfig : times) {
            if (timeConfig.contains("-")) {
                String[] ts = timeConfig.split("-");
                String t1 = ts[0];
                String t2 = ts[1];
                LocalTime start = t1.contains(":") ? LocalTime.parse(t1) : LocalTime.parse(t1 + ":00");
                LocalTime end = t2.contains(":") ? LocalTime.parse(t2) : LocalTime.parse(t2 + ":00");
                if (now.isAfter(start) && now.isBefore(end)) {
                    return true;
                }
            }
        }
        return false;
    }

}
